#include "../../common/message.pb.h"
#include "../../server/ConsumerManager.hpp"
#include <gtest/gtest.h>
#include <unordered_map>
#include <string>

micromq::ConsumerManager::ptr manager;

class ConsumerTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        manager = std::make_shared<micromq::ConsumerManager>();
        manager->createConsumerCenter("q1");
    }

    virtual void TearDown() override
    {
        manager->clear();
    }
};

void cb(const std::string& tag, const micromq::BasicProperties* bp, const std::string& content)
{
    std::cout << "cb in..." << std::endl;
}

TEST(consumer_test, add_test)
{
    manager->addConsumerToMsgQueue("c1", "q1", false, cb);
    manager->addConsumerToMsgQueue("c2", "q1", false, cb);
    manager->addConsumerToMsgQueue("c3", "q1", false, cb);

    ASSERT_EQ(manager->isExists("c1", "q1"), true);
    ASSERT_EQ(manager->isExists("c2", "q1"), true);
    ASSERT_EQ(manager->isExists("c3", "q1"), true);
}

TEST(consumer_test, remove_test)
{
    manager->removeConsumerFromMsgQueue("c3", "q1");
    ASSERT_EQ(manager->isExists("c3", "q1"), false);
}

TEST(consumer_test, get_test)
{
    micromq::Consumer::ptr consumer = manager->getConsumerFromMsgQueue("q1");
    ASSERT_NE(consumer.get(), nullptr);
    ASSERT_EQ(consumer->_tag, std::string("c1"));
    consumer = manager->getConsumerFromMsgQueue("q1");
    ASSERT_NE(consumer.get(), nullptr);
    ASSERT_EQ(consumer->_tag, std::string("c2"));
    consumer = manager->getConsumerFromMsgQueue("q1");
    ASSERT_NE(consumer.get(), nullptr);
    ASSERT_EQ(consumer->_tag, std::string("c1"));
}

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ConsumerTest);
    RUN_ALL_TESTS();

    return 0;
}